package com.neuedu.studentmanage.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.Spends;
import com.neuedu.studentmanage.service.SpendsService;

@Controller
public class SpendsController {
	@Autowired
	private SpendsService spendsService;

	@GetMapping("spends/index")
	public String spendsIndex() {
		return "admin/spends";
	}

	// 返回数据库数
	// easy ui请求时使用的是post方法，这里需要使用post方法
	// easy ui进行分页查询时，使用page传递查询的页码，使用rows传递查询的页码
	@PostMapping("spends/list")
	@ResponseBody
	public Pager<Spends> spendsList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "5") Integer rows) {
		Pager<Spends> pager = new Pager<>(page, rows);
		// 根据条件进行分页查询
		List<Spends> spendsList = spendsService.findListByPager(pager);
		Integer total = spendsService.findTotalByPager(pager);

		pager.setRows(spendsList);
		pager.setTotal(total);// 把total存进来
		return pager;
	}
	
	@RequestMapping("spends/add")
	@ResponseBody
	public Result doAdd(Spends spends) {
		Result result=spendsService.add(spends);
		return result;
	}
	
	@RequestMapping("spends/edit")
	@ResponseBody
	public Result doEdit(Spends spends) {
		Result result=spendsService.edit(spends);
		return result;
	}
	
	@RequestMapping("spends/delete")
	@ResponseBody
	//admin接收传参
	public Result doDelete(Integer cardId) {
		List<Spends> list=new ArrayList<Spends>();
		list.add(new Spends(cardId));
		
		Result result= spendsService.deleteByIds(list);
		return result;
	}

}
